package com.ht.dsp.impl;

import com.ht.aems.util.FTPOperator;
import com.ht.aems.util.SocketManager;
import com.ht.dsp.RtdbRefresher;
import org.apache.log4j.Logger;

import java.net.InetSocketAddress;
import java.rmi.RemoteException;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2010-1-23
 */
public class ScadaDataUpdator_ge extends AbstractScadaUpdator {
    private static Logger log = Logger.getLogger(ScadaDataUpdator_ge.class);

    private static InetSocketAddress ip1 = new InetSocketAddress("192.168.181.13", 8374);
    private static InetSocketAddress ip2 = new InetSocketAddress("192.168.182.13", 8374);
    private static InetSocketAddress ip3 = new InetSocketAddress("192.168.181.14", 8374);
    private static InetSocketAddress ip4 = new InetSocketAddress("192.168.182.14", 8374);

    private int failedNum = 0;
    private InetSocketAddress currentIp = ip1;

    public ScadaDataUpdator_ge() throws RemoteException {
        super();
        setInstance("ge");
    }

    @Override
    public String getMeasureId(String input) {
        return input;//todo: wrong
    }

    public boolean update() {
        boolean r = order();
        if (!r) {
            log.warn("Updating scada data failed!");
            failedNum++;
            if (failedNum >= failNumAllowed) {
                SocketManager.closeSocket(RTDB_SERVER);
                if (currentIp == ip1)
                    currentIp = ip2;
                else if (currentIp == ip2) {
                    currentIp = ip3;
                } else if (currentIp == ip3) {
                    currentIp = ip4;
                } else if (currentIp == ip4)
                    currentIp = ip1;
                SocketManager.register(RTDB_SERVER, currentIp);
            }
            return false;
        } else {
            failedNum = 0;
            RtdbRefresher.saveRawDataToRtdb(getInstance());
            return true;
        }
    }

    public void updateSE() throws RemoteException {
        boolean r = downloadSEFile("192.168.181.3", "192.168.181.4");
        if (!r)
            downloadSEFile("192.168.182.3", "192.168.182.4");
    }


    private boolean downloadSEFile(String ip1, String ip2) {
        String localFileName = "/aems/datastore/se/";
        FTPOperator ftp = new FTPOperator();
        boolean result = false;
        try {
            ftp.connectServer(ip1, 21, "xa21", "xa21", "/xalocal/data/ps/psa/rt");
            ftp.download("pnahc.d", localFileName + "pnahc.d1");
            ftp.closeServer();
            result = true;
            log.info("Success to get se file from " + ip1);
        } catch (Exception e) {
            log.warn("Failed to get se file from " + ip1);
        }
        try {
            ftp.connectServer(ip2, 21, "xa21", "xa21", "/xalocal/data/ps/psa/rt");
            ftp.download("pnahc.d", localFileName + "pnahc.d2");
            ftp.closeServer();
            result = true;
            log.info("Success to get se file from " + ip2);
        } catch (Exception e) {
            log.warn("Failed to get se file from " + ip2);
        }
        return result;
    }
}
